﻿using Newtonsoft.Json;
using SouthKoreaCoffee.Model;
using SouthKoreaCoffee.RobotModel;
using SouthKoreaCoffee.SocketClientsModel;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SouthKoreaCoffee.IOModel
{
    public class IOMsg
    {
        private static IOMsg instance;
        private IOMsg() { }

        public static IOMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsg();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        public bool isRobotBol = false;
        string nostrs = "";
        public bool CFBol = true;
        public Thread All_thr;
        /// <summary>
        /// 左臂点位全部关闭
        /// </summary>
        public void QK()
        {
            try
            {
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJB1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJB2_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotCCWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJCJB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJGT_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQDY_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotDYWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKFWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotKSZZ_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJBCG_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJBSB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJKF_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJBWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotGTWC_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotWNKF_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQZS_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB2_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB3_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC2_Down();
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 弃杯清位
        /// </summary>
        public void QBQK()
        {
            try
            {
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB2_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB3_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQQB_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC1_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQCC2_Down();
                Thread.Sleep(100);
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 机器人功能按钮关闭
        /// 状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        public void RobotQK(double type = 12)
        {
            try
            {
                if (type == 12 || type == 11)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRTZYXL_Down();
                }
                if (type == 12 || type == 7)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRYXL_Down();
                }
                if (type == 12 || type == 9)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRBJQRL_Down();
                }
                if (type == 12 || type == 3)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRSDL_Down();
                }
                if (type == 12 || type == 4)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRXDL_Down();
                }
                if (type == 12 || type == 5)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRSNL_Down();
                }
                if (type == 12 || type == 6)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRXSNL_Down();
                }
                if (type == 12 || type == 10)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRZTL_Down();
                }
                if (type == 12 || type == 8)
                {
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJQRJXYXL_Down();
                }
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IOSerial_Robot_Thr()
        {
            try
            {
                bool isRobot = false;
                RobotSerialPortLeft.Instance.Initialization(IPorPortInfo.Instance.RobotIPLeft, IPorPortInfo.Instance.RobotPortLeft);
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRSDL_UP();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRSNL_UP();
                Thread.Sleep(1000);
                RobotModelLeft.Instance.RobotJQRSDL_Down();
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotJQRSNL_Down();
                Thread.Sleep(100);
                while (!isRobot)
                {
                    isRobot = RobotModelLeft.Instance.LRobotJQRYSN_Sel();
                    Thread.Sleep(100);
                }
                Instance.QK();
                Instance.RobotQK();
                isRobotBol = true;
            }
            catch (Exception)
            {
            }
        }
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortNameDO)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IOSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.PortNameDO, 38400, "", 8, 1);//其他串口通讯
                IOSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                Robot_Thr();
            }
            catch (Exception)
            {
                IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        public void Robot_Thr()
        {
            try
            {
                RobotOperate.Instance.Robot_Initialisation_double(); //机器人初始化
            }
            catch (Exception)
            {
                Thread.Sleep(100);
                Robot_Thr();
            }
        }
        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                var dd = IOSerialPort.Instance.serialPort.BytesToRead;
                byte[] result = new byte[dd];
                IOSerialPort.Instance.serialPort.Read(result, 0, dd);
                string str = "";// $"{DateTime.Now}：\n";
                str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
    }
}
